<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    <el-breadcrumb-item>用户列表</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card>
    <el-row :gutter="20">
      <el-col :span="10">
        <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @clear="getUserList">
          <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
        </el-input>
      </el-col>
<el-col :span="2"><el-button type="primary">添加用户</el-button></el-col>
    </el-row>
    <el-table :data="userList" style="width: 100%" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="username" label="姓名"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column prop="role_name" label="角色"></el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.mg_state" @change="changeState(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
          <el-tooltip class="item" effect="dark" content="分配角色" placement="top" :enterable="false">
            <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryinfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
  </el-card>
</div>
</template>

<script>
    export default {
        name: "user",
      data(){
          return{
            queryinfo:{query:'', pagenum:1, pagesize:2},
            userList:[],
            total:0
          }

      },
      created() {
          this.getUserList();
      },
      methods:{
        async getUserList(){
          const res=await this.$http.get('/users',{params:this.queryinfo})
          if(res.data.meta.status!=200){this.$message.error('获取列表失败')}
          else {
          this.userList=res.data.data.users;
          this.total=res.data.data.total;
          }
        },
        handleSizeChange(newSize){
          this.queryinfo.pagesize=newSize;
          this.getUserList();
        },
        handleCurrentChange(newnum){
          this.queryinfo.pagenum=newnum;
          this.getUserList();
        },
       async changeState(row){
          const restate=await this.$http.put(`users/${row.id}/state/${row.mg_state}`)
         if(restate.data.meta.status!=200){
           row.mg_state=!row.mg_state
           this.$message.error('用户状态修改失败')}
         else{this.$message.success('状态修改成功')}
        }
      }
    }
</script>

<style scoped>

</style>
